package com.hframe.image;

import com.hframe.utils.BytesUtils;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


/**
 * 保存图片
 *
 * @author Administrator
 */
public class ImageUtils {
    private static Logger log = Logger.getLogger(ImageUtils.class);

    /**
     * @param bs
     * @param path
     * @return
     */
    public static boolean bmpBytesToPng(byte[] bs, String path) {
        if (null == bs || bs.length < 14)
            return false;

        if (0x4D42 != BytesUtils.getShort(bs) || BytesUtils.getInt(bs, 2) < bs.length)
            return false;

        int bfOffBits = BytesUtils.getInt(bs, 10);
        int width = BytesUtils.getInt(bs, 18);
        int height = BytesUtils.getInt(bs, 22);
        if (32 != BytesUtils.getShort(bs, 28) || width < 0 || height < 0)
            return false;

        int[] buffer = new int[width * height];
        for (int h = height - 1; h >= 0; h--) {
            for (int w = 0; w < width; w++) {
                buffer[h * width + w] = BytesUtils.getInt(bs, bfOffBits);
                bfOffBits += 4;
            }
        }
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        try {
            image.setRGB(0, 0, width, height, buffer, 0, width);
            ImageIO.write(image, "png", new File(path));
            return true;
        } catch (IOException e) {
            log.error(e, e);
            return false;
        }
    }

    public static BufferedImage scaleBitmap(BufferedImage bitmap, float width, float height, Boolean standard) {
        float scale;
        if (null == standard) {
            scale = (width / bitmap.getWidth() > height / bitmap.getHeight()) ?
                    width / bitmap.getWidth() : height / bitmap.getHeight();

        } else if (standard) {
            scale = width / bitmap.getWidth();
        } else {
            scale = height / bitmap.getHeight();
        }
        width *= scale;
        height *= scale;
        BufferedImage _image = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
        _image.getGraphics().drawImage(bitmap, 0, 0, (int) width, (int) height, null); // 绘制缩小后的图
        return _image;
    }

    public static BufferedImage clipBitmap(BufferedImage bitmap, float width, float height, Boolean standard) {
        float scale;
        if (null == standard) {
            scale = (height / bitmap.getHeight()) < width / bitmap.getWidth() ?
                    width / bitmap.getWidth() : height / bitmap.getHeight();

        } else if (standard) {
            scale = width / bitmap.getWidth();
        } else {
            scale = height / bitmap.getHeight();
        }

        float w = bitmap.getWidth() * scale;
        float h = bitmap.getHeight() * scale;

        BufferedImage image = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_RGB);
        image.getGraphics().drawImage(bitmap, (int) ((width - w) / 2), (int) ((height - h) / 2), (int) w, (int) h, null); // 绘制缩小后的图
        return image;
    }
}
